//
//  DHDetailRadioViewController.m
//  Moment
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 片刻. All rights reserved.
//

#import "DHDetailRadioViewController.h"
#import "DHFirstTableViewCell.h"
#import "DHDetailRadioTableViewCell.h"
@interface DHDetailRadioViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, assign)NSInteger nextPage;
@property (nonatomic, retain)UITableView *myTabelView;
@property (nonatomic, retain)NSMutableArray *subRadioArr;
@property (nonatomic, retain)DHRadioInfo *radioInfo;
@property (nonatomic, retain)UIImageView *headImageView;

@property (nonatomic, retain)NSMutableArray *refreshPicArr;
@property (nonatomic, assign)BOOL isUpLoad;

@end

@implementation DHDetailRadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _subRadioArr = [NSMutableArray array];
    _refreshPicArr = [NSMutableArray array];
    
    [self createViews];
   
    [self addFooterRefresh];
    // Do any additional setup after loading the view.
}
-(void)createViews{
    _myTabelView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    _myTabelView.delegate = self;
    _myTabelView.dataSource = self;
    [_myTabelView registerClass:[DHFirstTableViewCell class] forCellReuseIdentifier:@"DHFirstTableViewCell"];
    [_myTabelView registerClass:[DHDetailRadioTableViewCell class] forCellReuseIdentifier:@"DHDetailRadioTableViewCell"];
    [self.view addSubview:_myTabelView];
    UIImage *image = [UIImage imageNamed:@"back.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(LeftBtnAction)];
    _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375*WIDTH, 180*HEIGHT)];
    _myTabelView.tableHeaderView = _headImageView;
    
    for (int i = 0; i < 29; i++) {
        NSString *imageName = [NSString stringWithFormat:@"refresh%d.png",i];
        UIImage *image = [UIImage imageNamed:imageName];
        CGSize size = CGSizeMake(20, 20);
        UIImage *newImage = [SelfAdaption image:image byScalingToSize:size];
        [_refreshPicArr addObject:newImage];
    }

}
-(void)addFooterRefresh
{
    //    设置上拉刷新动画
    MJRefreshAutoGifFooter  *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    [footer setImages:_refreshPicArr forState:MJRefreshStateRefreshing];
    footer.refreshingTitleHidden = YES;
    footer.stateLabel.hidden = YES;
    
    self.myTabelView.mj_footer = footer;
    [self.myTabelView.mj_footer beginRefreshing];
}
-(void)refreshAction{
    NSLog(@"上拉");
    self.nextPage += 10;
    self.isUpLoad = YES;
    // 重新请求数据
    [self getUpData];
}


-(void)LeftBtnAction{
    [self.navigationController popViewControllerAnimated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        return 100 * HEIGHT;
    }else{
        return 60 * HEIGHT;
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        return _subRadioArr.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *firstCell = @"DHFirstTableViewCell";
    
    static NSString *detailRadioCell = @"DHDetailRadioTableViewCell";
    DHFirstTableViewCell *firstTabelCell= [tableView dequeueReusableCellWithIdentifier:firstCell];
    firstTabelCell.selectionStyle = UITableViewCellAccessoryNone;


    DHDetailRadioTableViewCell *detailRadioTabelCell = [tableView dequeueReusableCellWithIdentifier:detailRadioCell];
    
        detailRadioTabelCell.selectionStyle = UITableViewCellAccessoryNone;
    if (indexPath.section == 0) {
        firstTabelCell.radioInfo = _radioInfo;
        
        return firstTabelCell;
    }else{
        DHSubRadio *subRadio = [[DHSubRadio alloc]init];
        subRadio = _subRadioArr[indexPath.row];
        subRadio.tag = indexPath.row;
        subRadio.stationName = _radio.title;
        _subRadioArr[indexPath.row] = subRadio;
        detailRadioTabelCell.radio = subRadio;
        return detailRadioTabelCell;
    }
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
     
        DHMusicPlayerViewController *musicVC = [DHMusicPlayerViewController shareMusicPlayerViewController];
        [DHMusicPlayerViewController shareMusicPlayerViewController].marker = @"detail";
        [DHMusicPlayerViewController shareMusicPlayerViewController].figure = indexPath.row;
        DHSubRadio *subRadio = _subRadioArr[indexPath.row];
        [DHMusicPlayerViewController shareMusicPlayerViewController].tingID = subRadio.tingid;
        [DHMusicPlayerViewController shareMusicPlayerViewController].radioArr = _subRadioArr;
        [self.navigationController pushViewController:musicVC animated:YES];
    }
  
    
}
-(void)setRadio:(DHRadio *)radio{
    if (_radio != radio) {
        _radio = radio;
    }
    _radioID = radio.radioid;


 [self getAFNetWorkingDataIsUpLoad];
}
-(void)setCarousel:(DHCarousel *)carousel{
    if (_carousel != carousel) {
        _carousel = carousel;
    }
    _radioID = [_carousel.url substringFromIndex:12];

 [self getAFNetWorkingDataIsUpLoad];
}
- (void)getAFNetWorkingDataIsUpLoad{
    NSString *urlStr = @"http://api2.pianke.me/ting/radio_detail";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=D4DBEEC7-7A09-433B-9EA4-D885DD84AC5F&radioid=%@&version=3.0.6", _radioID];

    [GetData postUrl:urlStr body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray *array = [bigDic objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            DHSubRadio *subRadio = [[DHSubRadio alloc]init];
            [subRadio setValuesForKeysWithDictionary:dic];
            [_subRadioArr addObject:subRadio];
        }
        NSMutableDictionary *radioinfo = [NSMutableDictionary dictionary];

        radioinfo = [bigDic objectForKey:@"radioInfo"];
        _radioInfo = [[DHRadioInfo alloc]init];
        [_radioInfo setValuesForKeysWithDictionary:radioinfo];
        self.navigationItem.title = _radioInfo.title;
        NSURL *url = [NSURL URLWithString:_radioInfo.coverimg];
        [_headImageView sd_setImageWithURL:url placeholderImage:PLACEHOLDER_IMAGE];
        [_myTabelView reloadData];
    } failure:^(NSError *error) {
    }];
}
#pragma 上拉加载
- (void)getUpData{
    NSString *urlStr = @"http://api2.pianke.me/ting/radio_detail_list";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&limit=10&radioid=%@&start=%ld&version=3.0.6", _radioID, _nextPage];
    [GetData postUrl:urlStr body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray *array = [bigDic objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            DHSubRadio *radio = [[DHSubRadio alloc]init];
            [radio setValuesForKeysWithDictionary:dic];
            [_subRadioArr addObject:radio];
        }
        [_myTabelView reloadData];
        [_myTabelView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
